# -*- coding: UTF-8 -*-
import os
import sys
import logbook
from logbook import Logger,StreamHandler, TimedRotatingFileHandler
import socket

HostName = socket.gethostname()
ServiceName = "ai_detect_service"


def log_type(record,handler):
    log = "[{date}] [{level}] [{filename}] [{func_name}] [{lineno}] [{msg}]".format(
        date = record.time,                              # 日志时间
        level = record.level_name,                       # 日志等级
        filename = os.path.split(record.filename)[-1],   # 文件名
        func_name = record.func_name,                    # 函数名
        lineno = record.lineno,                          # 行号
        msg = record.message,                            # 日志内容

    )
    return log

def get_logger():
    logbook.set_datetime_format('local')
    StreamHandler(sys.stdout, encoding='utf-8', bubble=True).push_application()
    cur_path = os.path.dirname(os.path.realpath(__file__))
    cur_log_path = cur_path + "/log"
    if os.path.exists(cur_log_path) is not True:
        os.mkdir(cur_log_path)
    # handler = RotatingFileHxandler(cur_log_path + '/' + ServiceName + '.log', bubble=True, max_size=50000,
    #                               backup_count=10)
    handler = TimedRotatingFileHandler(
        cur_log_path + '/' + ServiceName + '.log',
        date_format='%Y-%m-%d',
        rollover_format='{basename}{ext}.{timestamp}',
        backup_count=10,
        bubble=False)

    handler.formatter = log_type
    handler.push_application()
    logger = Logger("ai_detect_service")
    return logger


LOG = get_logger()






